package bsc.war.ajax;

import bsc.ejb.entity.ColorimetrySettings;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.Framework;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class DesignerAddScoreCard extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            //Long idScoreCard = request.getParameter("scoreCard") != null && NumberUtils.isNumber(request.getParameter("scoreCard")) ? Long.parseLong(request.getParameter("scoreCard")) : null;
            //ScoreCard scoreCard = delegate.findScoreCard(idScoreCard);
            String type = request.getParameter("type");

            List<Person> persons = delegate.findAvaliablePerson();
            Framework framework = delegate.findFramework(Boolean.TRUE);


            List<Colorimetry> colorimetries = delegate.findColorimetry(framework, Boolean.TRUE);
            HashMap<Colorimetry, Double> minColorValue = new HashMap<Colorimetry, Double>();
            HashMap<Colorimetry, Double> maxColorValue = new HashMap<Colorimetry, Double>();


            String types[] = {"Area", "Employee", "Theme", "Objective", "Perspective"};


            ScoreCard scoreCard = null;

            if ("edit".equals(type) && NumberUtils.isDigits(request.getParameter("id"))) {

                scoreCard = delegate.findScoreCard(Long.valueOf(request.getParameter("id")));
                request.setAttribute("responsable", scoreCard.getResponsible());

                //Colorimetries
                List<ColorimetrySettings> settings = delegate.findColorimetrySettings(scoreCard);
                for (ColorimetrySettings setting : settings) {
                    minColorValue.put(setting.getColorimetry(), setting.getMinScore()*100);
                    maxColorValue.put(setting.getColorimetry(), setting.getMaxScore()*100);
                }
            }

            request.setAttribute("scoreCard", scoreCard);
            request.setAttribute("persons", persons);
            request.setAttribute("colorimetries", colorimetries);
            request.setAttribute("types", types);
            request.setAttribute("minColorValue",minColorValue);
            request.setAttribute("maxColorValue",maxColorValue);
            //request.setAttribute("mapdrawings", delegate.findMapDrawing());
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Caught exception: " + e);
        }

        return new ModelAndView("designerAddScoreCard");
    }
}
